//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#include <aura.h>

static void threadEntry(Aura_PVoid pArgs)
{
    Aura_ThreadId threadId = AURA_INVALID_TID;
    AuraThread_GetCurrentId(&threadId);
    Aura_printf(" <child> : thread id = %d\n", threadId);
}

Aura_Bool AuraTest_Join_After_thread_Death()
{
    Aura_ThreadId threadId = AURA_INVALID_TID;
    Aura_Handle hThread = AURA_NULL;
    Aura_ECode ec = AURA_NOERROR;

    ec = AuraThread_Create(threadEntry, AURA_NULL, 0,  &hThread, &threadId);
    if (AURA_FAILED(ec)) {
        Aura_printf("File: %s Line: %d, AuraThread_Create. (ec = 0x%x)\n",
            __FILE__,__LINE__, ec);
        goto ErrorExit;
    }
    Aura_printf(" father: new threadId id = %d\n", threadId);

    Aura_WaitResult wr;
    AuraThread_Sleep(1000, &wr);

    ec = AuraThread_Join(hThread, AURA_INFINITE, &wr);
    if (AURA_FAILED(ec) || (Aura_WaitResult_OK != wr)) {
        Aura_printf("File: %s Line: %d, AuraThread_Join. (ec = 0x%x)\n",
            __FILE__,__LINE__, ec);
        goto ErrorExit;
    }

    Aura_QuitCode quitCode;
    ec = AuraThread_GetQuitCode(hThread, &quitCode);
    if (AURA_FAILED(ec) || AURA_FAILED(quitCode)) {
        Aura_printf("File: %s Line: %d, AuraThread_GetQuitCode. (ec = 0x%x)\n",
            __FILE__,__LINE__, ec);
        goto ErrorExit;
    }

    ec = AuraThread_Destroy(hThread);
    hThread = AURA_NULL;
    return AURA_TRUE;

ErrorExit:
    AuraThread_Destroy(hThread);
    hThread = AURA_NULL;
    return AURA_FALSE;
}

